import random
from sre_constants import error

from django.shortcuts import render
from rest_framework.response import Response
from rest_framework.views import APIView
import re

from utils.Authentication import MyAuthenticate
from utils.MyJWT import myjwt

from user.models import UserModel
from user.ser import UserSer
from utils.MyRedis import myRedis
from utils.Sendcode import send_message


# Create your views here.
class RegisterView(APIView):
    def post(self,request):
        account = request.data.get('account')
        password = request.data.get('password')
        mata = {}
        if account == '' or password == '':
            return Response({"code":400,"msg": "账号或密码不能为空"})

        if re.match(r"^[a-zA-Z0-9]{4,9}@(qq|163|sina|139)\.com$",account):
            mata['email'] = account
        elif re.match(r"^1[3-9]\d{9}$",account):
            users = UserModel.objects.filter(phone=account)
            if users.__len__() ==0:
                mata['phone'] = account
            else:
                return Response({"code":400,"msg":"手机号不允许重复"})
        else:
            mata['username'] = account
        mata['password'] = password
        ser = UserSer(data=mata)
        if ser.is_valid():
            ser.save()
        return Response({"code":200,"msg":"注册成功"})
class LogByAccountView(APIView):
    def post(self,request):
        account = request.data.get('account')
        password = request.data.get('password')
        if account == '' or password == '':
            return Response({"msg":"账号或密码不能为空",'code':400})
        if re.match(r"^1[3-9]\d{9}$",account):
            users = UserModel.objects.filter(phone=account)
            return Response({"msg":"手机号不存在","code":400})
        elif re.match(r"^[a-zA-Z0-9]{4,16}@(qq|163|sian|139)\.coom$",account):
            users = UserModel.objects.filter(encode=account)
            return Response({"msg":"邮箱不存在","code":400})
        else:
            users = UserModel.objects.filter(username=account)
        if users:
            if users[0].password == password:
                token = myjwt.getToken(users[0].id,account)
                return Response({"msg": "登录成功","code":200,'token':token,"id":users[0].id})
            else:
                return Response({"msg": "密码不正确", "code": 400})
        else:
            return Response({"msg":"账号不正确","code":400,"data":""})

class RedisView(APIView):
    def get(self,request):
        myRedis.set("code","105896",60)
        return Response({"code":myRedis.get('code')})

class toCodeView(APIView):
    def post(self,request):
        account = request.data.get('account')
        if re.match(r'^1[3-9]\d{9}$',account):
            code = str(random.randint(1000,9999))
            # send_message(account,code)
            myRedis.set(account,code,60)
            return Response({"msg":"发送成功",'code':myRedis.get(account)})
        else:
            return Response({"msg":"手机号不正确"})
class LoginCodeView(APIView):
    def post(self,request):
        account = request.data.get('account')
        code = request.data.get('code')
        if code == myRedis.get(account):
            if re.match(r"^1[1-9]\d{9}$",account):
                users = UserModel.objects.filter(phone=account)
                if users:
                    return Response({"msg":"登陆成功","token":myjwt.getToken(users[0].id,account),"id":users[0].id})
                else:
                    data = {"phone":account,"password":"000000"}
                    ser = UserSer(data=data)
                    if ser.is_valid():
                        ser.save()
                        return Response({"msg": "登陆成功","token":myjwt.getToken(users[0].id,account),"id":users[0].id})
                    else:
                        return Response({"msg":error})
        else:
            return Response({"msg":"验证码不正确"})
class Test(APIView):
    authentication_classes = [MyAuthenticate]
    def get(self,request):
        return Response("ok")